package com.forlove.app.qselect.login.presenter;

import android.text.TextUtils;

import com.forlove.app.common.base.mvp.presenter.IBasePresenter;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.qselect.CommonEntity;
import com.forlove.app.qselect.login.service.ISetAccountService;
import com.forlove.app.qselect.login.service.impl.SetAccountServiceImpl;
import com.forlove.app.qselect.login.view.SetAccountView;

import java.util.Map;

/**
 * Created by yjl on 2017/10/22.
 */

public class SetAccountPresenter extends IBasePresenter {
    private SetAccountView setAccountView;

    private ISetAccountService setAccountService;

    public SetAccountPresenter(SetAccountView setAccountView) {
        this.setAccountView = setAccountView;
        if (setAccountService == null) {
            setAccountService = new SetAccountServiceImpl();
            setService(setAccountService);
        }

    }

    public void loadSetAccountData(Map param) {

        setAccountService.loadData(ServerManager.ACTION_SET_ACCOUNT, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "success 设置账号：" + result);
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (setAccountView != null && commonEntity.state == 1) {
                    setAccountView.updateSetAccountView(NetworkManager.ResultCode.RESULT_SUCCESS,
                            commonEntity.message);
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                LogUtil.e("yjl", "onfail 设置账号：" + result);
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (setAccountView != null) {
                        setAccountView.updateSetAccountView(null, commonEntity.message);
                    }
                } else {
                    if (setAccountView != null) {
                        setAccountView.updateSetAccountView(null, "稍后重试!");
                    }
                }
            }
        });
    }

    public void upLoadPic(Map param) {
        setAccountService.uploadFile(ServerManager.ACTION_UPLOAD_IMAGE, param, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, String result) {
                LogUtil.e("yjl", "上传头像:" + result);
                String photoUrl = ParseHelper.getString(result, "data.img_name");
                if (setAccountView != null) {
                    setAccountView.updateSetPhotoView(photoUrl);
                }
            }

            @Override
            public void onFail(int statusCode, String result) {
                LogUtil.e("yjl", "上传头像 onFail:" + result);
                CommonEntity commonEntity = new CommonEntity();
                commonEntity.parseJSONString(result);
                if (!TextUtils.isEmpty(commonEntity.message)) {
                    if (setAccountView != null) {
                        setAccountView.updateSetAccountView(null, commonEntity.message);
                    }
                } else {
                    if (setAccountView != null) {
                        setAccountView.updateSetAccountView(null, "稍后重试!");
                    }
                }
            }
        });
    }
}
